﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_updateNews : System.Web.UI.Page
{
    db_news news = new db_news();
    protected void Page_Load(object sender, EventArgs e)
    {
        Server.Execute("inc/checkPurview.aspx");  // 此处用户权限验证

        if (Request.QueryString["id"] == null)
        {
            Response.Redirect("manageNews.aspx");
        }

        
        if (!IsPostBack)
        {
          //  this.bind_ddl();
            this.bind_news();
        }
    }
    
    public void bind_news() {
        string newsID = Request.QueryString["id"];
        DataSet ds_news = new DataSet();
        ds_news = news.selectNewsONE(newsID);
        if (ds_news.Tables.Count != 0)
        {
            tb_title.Text = ds_news.Tables[0].Rows[0]["title"].ToString();
            tb_body.Text = ds_news.Tables[0].Rows[0]["newsContent"].ToString();
            ddl_newStatus.SelectedValue = ds_news.Tables[0].Rows[0]["status"].ToString();
        }
    }
    protected void btn_ok_Click(object sender, EventArgs e)
    {
        string newsID = Request.QueryString["id"];
        string title = tb_title.Text.Trim();;
        string body = tb_body.Text.Replace("\'","''");
        string status = ddl_newStatus.SelectedValue;
        string slidetext = "";
        string[] picUrls = Tools.GetHtmlImageUrlList(body);
        string isPic;
        string picUrl;
        bool isSlide;
        if (picUrls.Length > 0)
        {
            isPic = "0";
            picUrl = picUrls[0];
            string nohtml = Tools.NoHTML(body);
            if (nohtml.Length > 20)
            {
                slidetext = nohtml.Substring(0, 20) + "……";
            }
            else
            {
                slidetext = nohtml + "……";
            }

            isSlide = true;
        }
        else
        {
            isPic = "1";
            picUrl = "";
            isSlide = false;
        }
        if (Page.IsValid)
        {
            if (news.updateNews(newsID, title, body, status, isPic, picUrl))
            {
                if (isSlide)
                {
                    Response.Write("<script language=javascript>alert('请重新生成幻灯片或修改幻灯片。\\n新闻更新成功！');location.href='manageExpress.aspx';</script>");
                }
                else
                {
                    Response.Write("<script language=javascript>alert('新闻更新成功！');location.href='manageNews.aspx';</script>");
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('失败！');</script>");
            }
        }
    
    }
    protected void btn_res_Click(object sender, EventArgs e)
    {
        this.bind_news();
    }
}